﻿<template>
  <div>
    <el-table :data = 'table.tableData' :max-height = 'tableHeight' style = 'width:100%;' stripe border empty-text = '该项任务还未开始，没有相关的审核信息！' v-loading = 'loading'>
      <el-table-column v-for = '(item, index) in table.tableColumn' :key = 'index' :label = 'item.text' align = 'center' show-overflow-tooltip>
        <template slot-scope='scope'>
          <span>{{scope.row[item.indexName]}}</span>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  data () {
    return {
      loading: false,
      table: {
        tableData: [],
        tableColumn: [
          { indexName: 'userName', text: '操作人' },
          { indexName: 'actionText', text: '当前步骤' },
          { indexName: 'operationTime', text: '操作时间' },
          { indexName: 'status', text: '当前状态' },
          { indexName: 'remark', text: '备注' }
        ]
      }
    }
  },
  props: ['isShow', 'primaryKey'],
  created () {
    this.initPage()
  },
  computed: {
    tableHeight: function () {
      return window.innerHeight / 2
    }
  },
  methods: {
    // 初始化该页面
    initPage () {
      if (this.isShow === 'stepsInfo') {
        this.table.tableData = []
        if (this.primaryKey) {
          this.loading = true
          this.getWorkflowStepsInfo()
        }
      }
    },
    // 获取实例和步骤数据
    getWorkflowStepsInfo () {
      this.$workflowPost('/WorkFlowEngine/InstanceSteps', {
        PrimaryKey: this.primaryKey
      }).then(data => {
        this.loading = false
        let dataInfo = data.data.result
        if (dataInfo.code === 'failed') {
          this.$message({
            message: dataInfo.msg,
            type: 'error'
          })
        } else if (dataInfo.code === 'success') {
          if (dataInfo.data.steps.length) {
            let stepsArray = dataInfo.data.steps
            stepsArray.forEach(item => {
              let obj = {
                userName: item.UserName,
                // userName: item.UserName ? (item.UserName + '(' + item.UserID + ')') : item.UserID,
                operationTime: (item.last_time && item.Status !== 1) ? this.changeOpertaionTime(item.last_time) : '',
                actionText: item.ActionCode ? this.getActionText(item.ActionCode) : '',
                remark: item.Remark,
                status: item.Status === 1 ? '待办理' : (item.Status === 2 ? (item.ActionCode === 'Done' ? '已完成' : '已办理') : (item.Status === 3 ? '已终止' : (item.Status === 4 ? '挂起' : '')))
              }
              this.table.tableData.push(obj)
            })
          }
        }
      }).catch(error => {
        console.log(error)
      })
    },
    // 获取当前操作的状态
    getActionText (ActionCode) {
      let actionText = ''
      if (ActionCode === 'Submit') {
        actionText = '提交'
      } else if (ActionCode === 'Save') {
        actionText = '保存'
      } else if (ActionCode === 'Delete') {
        actionText = '删除'
      } else if (ActionCode === 'Pass') {
        actionText = '通过'
      } else if (ActionCode === 'Reject' || ActionCode === 'SelectReject') {
        actionText = '驳回'
      } else if (ActionCode === 'Stop') {
        actionText = '终止'
      } else if (ActionCode === 'Done') {
        actionText = '结束'
      }
      return actionText
    },
    // 改变日期的格式
    changeOpertaionTime (date) {
      const current = new Date(date)
      let year = current.getFullYear()
      let month = this.checkCounts(current.getMonth() + 1)
      let day = this.checkCounts(current.getDate())
      let hours = this.checkCounts(current.getHours())
      let mins = this.checkCounts(current.getMinutes())
      let seconds = this.checkCounts(current.getSeconds())
      return year + '-' + month + '-' + day + ' ' + hours + ':' + mins + ':' + seconds
    },
    // 让日期的格式为：HH:mm:ss
    checkCounts (counts) {
      if (counts < 10) {
        return '0' + counts
      } else {
        return counts
      }
    }
  },
  watch: {
    isShow: {
      handler (val) {
        this.initPage()
      },
      deep: true
    }
  }
}
</script>
